<template>
<view>
<view class="login">
	<view class="login_title">
		<text>快狗登录</text>
	</view>
	<view class="titleimg">
		<image class="image" src="/static/logoimg.png"></image>
	</view>
	<view :class="'login_user ' + nameFocus">
		<input type="text" placeholder="用户ID" placeholder-style="color:rgb(173,185,193)" :value="nowname" @input="bindUsername" @focus="onFocusName" @blur="onBlurName">
	</view>
	<view :class="'login_pwd ' + psdFocus">
		<input type="text" password placeholder="用户密码" placeholder-style="color:rgb(173,185,193)" :value="nowpsw" @input="bindPassword" @focus="onFocusPsd" @blur="onBlurPsd">
	</view>
	<view class="login_btn">
		<button hover-class="btn_hover" @tap="login">登录</button>
	</view>

	<view class="login_text">
		<view class="" @click="resg" style="font-size: 12px;color: #b1b1b1;">
			立即注册
		</view>
		<view class="" @click="uppas" style="font-size: 12px;color: #b1b1b1;">
			忘记密码
		</view>
	</view>
	<!-- <block data-type="template" data-is="toast" data-attr="..._toast_">
	<view class="toast_content_box">
		<view class="toast_content" v-if="isHidefil">
			<view class="toast_content_border"></view>
			<view class="toast_content_icon">
				<image class="toast_icon_img" src="/static/images/filled@2x.png"></image>
			</view>
			<view class="toast_content_text">{{content}}</view>
		</view>

		<view class="toast_content_box">
			<view class="toast_content" v-if="isHidescss">
				<view class="toast_content_border toast_success"></view>
				<view class="toast_content_icon">
					<image class="toast_icon_img" src="/static/images/success@2x.png"></image>
				</view>
				<view class="toast_content_text">{{content}}</view>
			</view>
		</view>
	</view>
</block> -->
</view>
</view>
</template>

<script>
let WebIM = require("../../utils/WebIM")["default"];
let __test_account__, __test_psword__;
let disp = require("../../utils/broadcast");
let runAnimation = true;

export default {
  data() {
    return {
		nowname:'',
		nowpsw:'',
		name: "",
		psd: "",
		grant_type: "password",
		psdFocus: "",
		nameFocus: "",
		avatar:''
    };
  },

  components: {},
  props: {},
  onLoad: function () {
    const me = this;
    const app = getApp().globalData;
    
    disp.on("em.error.passwordErr", function () {
      uni.showToast({title: "用户名或密码错误",icon:'none'});
    });
  },
 //  onShow() {
	//   this.name =uni.getStorageSync('name')
	//   this.psw =uni.getStorageSync('psw')
	//   this.nowname =uni.getStorageSync('denglu')
	//   this.nowpsw =uni.getStorageSync('denglumi')
 //  	if(uni.getStorageSync('name')==''&&uni.getStorageSync('psw')==''){
	// 	return
	// }else{
	// 	uni.request({
	// 		url: this.G_base + '/api/login',
	// 		data: {
	// 			account: this.name,
	// 			password: this.psw,
		
	// 		},
	// 		method: "POST",
	// 		header: {
	// 			'content-type': 'application/x-www-form-urlencoded'
	// 		},
	// 		success: (res) => {
	// 			console.log(res);
	// 			if (res.data.status == 400) {
	// 				uni.showToast({
	// 					title: res.data.msg,
	// 					icon:'none'
	// 				})
	// 			} else {
	// 				uni.showToast({
	// 					title: "登陆成功",
	// 				})
	// 				uni.setStorageSync('denglu',this.name)
	// 				uni.setStorageSync('denglumi',this.psw)
	// 				this.disabled = false
	// 				uni.setStorageSync("token", res.data.data.token)
	// 				uni.setStorage({
	// 				  key: "myUsername",
	// 				  data: __test_account__ || name.toLowerCase()
	// 				});
	// 				getApp().globalData.conn.open({
	// 				  apiUrl: WebIM.config.apiURL,
	// 				  user: __test_account__ || this.name.toLowerCase(),
	// 				  pwd: __test_psword__ || this.psw,
	// 				  grant_type: this.grant_type,
	// 				  appKey: WebIM.config.appkey
	// 				});
	// 				uni.switchTab({
	// 					url: "../chat/chat"
	// 				})
		
	// 			}
		
	// 		},
	// 		fail: (err) => {
	// 			console.log(err)
	// 		}
	// 	});
	// }
 //  },
  methods: {
	  //注册
	  resg() {
	  	uni.navigateTo({
	  		url: "/pages/login/register"
	  	})
	  },
	  //忘记密码
	  uppas() {
	  	uni.navigateTo({
	  		url: "/pages/login/forgetpsw"
	  	})
	  },
    bindUsername: function (e) {
      this.setData({
        name: e.detail.value
      });
    },
    bindPassword: function (e) {
      this.setData({
        psd: e.detail.value
      });
    },
    onFocusPsd: function () {
      this.setData({
        psdFocus: 'psdFocus'
      });
    },
    onBlurPsd: function () {
      this.setData({
        psdFocus: ''
      });
    },
    onFocusName: function () {
      this.setData({
        nameFocus: 'nameFocus'
      });
    },
    onBlurName: function (e) {
		
      this.setData({
        nameFocus: ''
      });
    },
    login: function () {
      runAnimation = !runAnimation;
		if(this.name==""||this.psd==''){
			this.name = this.nowname
			this.psd = this.nowpsw
		}
      if (!__test_account__ && this.name == "") {
        uni.showToast({title: "请输入用户名！",icon:'none'});
        return;
      } else if (!__test_account__ && this.psd == "") {
        uni.showToast({title: "请输入密码！",icon:'none'});
        return;
      }
		uni.request({
			url: this.G_base + '/api/login',
			data: {
				account: this.name,
				password: this.psd,
		
			},
			method: "POST",
			header: {
				'content-type': 'application/x-www-form-urlencoded'
			},
			success: (res) => {
				console.log(res);
				if (res.data.status == 400) {
					uni.showToast({
						title: res.data.msg,
						icon:'none'
					})
				} else {
					this.name=res.data.data.account
					uni.setStorageSync('name',this.name)
					uni.setStorageSync('psw',this.psd)
					uni.showToast({
						title: "登陆成功",
					})
					this.disabled = false
					uni.setStorageSync("token", res.data.data.token)
					uni.setStorage({
					  key: "myUsername",
					  data: __test_account__ || this.name.toLowerCase()
					});
					getApp().globalData.conn.open({
					  apiUrl: WebIM.config.apiURL,
					  user: __test_account__ || this.name.toLowerCase(),
					  pwd: __test_psword__ || this.psd,
					  grant_type: this.grant_type,
					  appKey: WebIM.config.appkey
					});
					uni.switchTab({
						url: "../chat/chat"
					})
		
				}
		
			},
			fail: (err) => {
				console.log(err)
			}
		});
    }
  }
};
</script>
<style>
@import "./login.css";
</style>